﻿using Services;
using Services.Input;
using Services.Window;
using UnityEngine;

namespace UI.Support
{
    public class Pause : MonoBehaviour
    {
        private IInputService _inputService;
        private PlayerControls _controls;
        private IWindowService _windowService;
        private bool _pausePressed;
        private GameObject _window;

        private void Start()
        {
            _inputService = AllServices.Container.Single<IInputService>();
            _windowService = AllServices.Container.Single<IWindowService>();
            _controls = _inputService.GetControls();
            _controls.Enable();
        }

        private void Update()
        {
            _pausePressed = _controls.UI.Pause.triggered;
            switch (_pausePressed)
            {
                case true when !_window:
                    OpenWindow();
                    break;
                case true when _window:
                    CloseWindow();
                    break;
            }
        }

        private void CloseWindow() =>
            Destroy(_window);

        private void OpenWindow() =>
            _window = _windowService.Open(WindowId.PauseWindow);
    }
}